package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.School;
import cn.sunlelin.educationserver.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @PostMapping("/school/createSchool")
    public Map<String,Object> createSchool(@RequestBody School school){
        Map<String,Object> map=new HashMap<>();
        if(schoolService.createSchool(school)){
            map.put("code",200);
            map.put("msg","添加学校信息成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","添加学校信息失败，该学校名已存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/school/updateSchool")
    public Map<String,Object> updateSchool(@RequestBody School school){
        Map<String,Object> map=new HashMap<>();
        if(schoolService.updateSchool(school)){
            map.put("code",200);
            map.put("msg","修改学校信息成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改学校信息失败，该学校名已存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/school/findAll")
    public Map<String,Object> findAll(){
        List<School> schoolList= schoolService.findAll();
        Map<String,Object> map=new HashMap<>();
        if(schoolList!=null){
            map.put("code",200);
            map.put("msg","查询所有学校信息成功");
            map.put("dataobject",schoolList);
        }
        else {
            map.put("code",500);
            map.put("msg","查询所有学校信息失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/school/selectSchoolById/{schoolId}")
    public Map<String,Object> selectSchoolById(@PathVariable Integer schoolId){
        School school=schoolService.selectSchoolById(schoolId);
        Map<String,Object> map=new HashMap<>();
        if(school!=null){
            map.put("code",200);
            map.put("msg","根据Id查询学校信息成功");
            map.put("dataobject",school);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查询学校信息失败，该Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/school/selectSchoolByName")
    public Map<String,Object> selectSchoolByName(String schoolName){
        School school=schoolService.selectSchoolByName(schoolName);
        Map<String,Object> map=new HashMap<>();
        if(school!=null){
            map.put("code",200);
            map.put("msg","根据Name查询学校信息成功");
            map.put("dataobject",school);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Name查询学校信息失败，该Name不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/school/deleteSchool/{schoolId}")
    public Map<String,Object> deleteSchool(@PathVariable Integer schoolId){
        Map<String,Object> map=new HashMap<>();
        if(schoolService.deleteSchool(schoolId)){
            map.put("code",200);
            map.put("msg","删除学校信息成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除学校信息失败，该Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }
}
